package net.huashitong.ssydt.provider.task.service;


import net.huashitong.ssydt.base.dao.LongMapper;
import net.huashitong.ssydt.base.service.AbstractLongService;
import net.huashitong.ssydt.exceptions.ServiceException;
import net.huashitong.ssydt.provider.task.mapper.SsTaskTaskRecordMapper;
import net.huashitong.ssydt.provider.utils.date.DateUtils;
import net.huashitong.ssydt.task.entity.SsTaskTask;
import net.huashitong.ssydt.task.entity.SsTaskTaskRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2018.01.22
 */
@Service
@Transactional
public class SsTaskTaskRecordServiceImpl extends AbstractLongService<SsTaskTaskRecord> implements SsTaskTaskRecordService {

    private SsTaskTaskRecordMapper mapper;

    private SsTaskTaskService ssTaskTaskService;

    @Autowired
    public SsTaskTaskRecordServiceImpl(SsTaskTaskRecordMapper mapper, SsTaskTaskService ssTaskTaskService) {
        this.mapper = mapper;
        this.ssTaskTaskService = ssTaskTaskService;
    }

    @Override
    protected LongMapper<SsTaskTaskRecord> getLongMapper() {
        return this.mapper;
    }

    @Override
    public SsTaskTaskRecord getNowDateAutoCreateByUserIdWidthTaskId(String userId, Long taskId) {
        SsTaskTaskRecord ssTaskTaskRecord = this.getByUserIdWidthTaskIdWidthBelongDate(userId, taskId, DateUtils.getDate());
        if (ssTaskTaskRecord == null) {
            SsTaskTask taskTask = ssTaskTaskService.selectByPrimaryKey(taskId);
            if (taskTask == null) {
                throw new ServiceException("对象 net.huashitong.ssydt.api.task.entity.SsTaskTask 不存在");
            }
            ssTaskTaskRecord = new SsTaskTaskRecord();
            ssTaskTaskRecord.setUserId(userId);
            ssTaskTaskRecord.setTaskId(taskId);
            ssTaskTaskRecord.setStatus("01");
            ssTaskTaskRecord.setGoldNumber(taskTask.getGoldNumber());
            ssTaskTaskRecord.setNumber(0);
            ssTaskTaskRecord.setBelongDate(DateUtils.getDate());
            this.insert(ssTaskTaskRecord);
        }
        return ssTaskTaskRecord;
    }

    @Override
    public SsTaskTaskRecord getByUserIdWidthTaskIdWidthBelongDate(String userId, Long taskId, String belongDate) {
        SsTaskTaskRecord record = new SsTaskTaskRecord();
        record.setUserId(userId);
        record.setTaskId(taskId);
        record.setBelongDate(belongDate);
        return this.mapper.getByUserIdWidthTaskIdWidthBelongDate(record);
    }

    @Override
    public void finishTask(String userId, long taskId) {
        SsTaskTaskRecord ssTaskTaskRecord = this
                .getNowDateAutoCreateByUserIdWidthTaskId(userId, taskId);
        if (ssTaskTaskRecord.getStatus().equals("01")) {
            ssTaskTaskRecord.setNumber(ssTaskTaskRecord.getNumber() + 1);
            SsTaskTask task = ssTaskTaskService.selectByPrimaryKey(ssTaskTaskRecord.getTaskId());
            if (ssTaskTaskRecord.getNumber() >= task.getNumber()) {
                ssTaskTaskRecord.setStatus("02");
            }
            this.updateByPrimaryKey(ssTaskTaskRecord);
        }
    }
}
